package com.buyantech.smweb.excel;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

import com.buyantech.smweb.model.CreditsExchange;
import com.buyantech.smweb.model.ObtainIntegral;
import com.buyantech.smweb.model.User;

public class ExcelAVieData extends AbstractExcelView{
	
	@Override
	protected void buildExcelDocument(Map model, HSSFWorkbook workbook,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
 
		List<String>headers = (List<String>)model.get("headers");
		String type=model.get("type")+"";
		
	    HSSFSheet sheet = workbook.createSheet("Sheet");
		HSSFRow header = sheet.createRow(0);
		for(int i=0;i<headers.size();i++){
			header.createCell(i).setCellValue(headers.get(i));
		}
		
		if(type.equals("1")){
			List<CreditsExchange> list=(List<CreditsExchange>)model.get("content");
			workSigninAdd(sheet,list);
		}else if(type.equals("2")){
			List<User> list=(List<User>)model.get("content");
			usreAdd(sheet,list);
		}else if(type.equals("3")){
			List<ObtainIntegral> list=(List<ObtainIntegral>)model.get("content");
			integralAdd(sheet,list);
		}
		
	 
	}
	
	/**
	 * 积分兑换记录
	 * @param sheet
	 * @param list
	 */
	public void workSigninAdd( HSSFSheet sheet,List<CreditsExchange> list){
		int rowNum = 1;
		for(CreditsExchange creditsExchange : list){
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(creditsExchange.getUserid());
			row.createCell(1).setCellValue(creditsExchange.getUsername());
			row.createCell(2).setCellValue(creditsExchange.getCrdate());
			row.createCell(3).setCellValue(creditsExchange.getTypeStr());
			row.createCell(4).setCellValue(creditsExchange.getIntegral());
			row.createCell(5).setCellValue(creditsExchange.getMoney());
			row.createCell(6).setCellValue(creditsExchange.getAccount());
			//状态(1兑换中，2支付成功，3Varhcar支付失败)
			String state="";
			if(creditsExchange.getState().equals("1")){
				state="兑换中";
			}else if(creditsExchange.getState().equals("2")){
				state="支付成功";
			}else{
				state="支付失败";
			}
			row.createCell(7).setCellValue(state);
			if(creditsExchange.getIsCheck()==1)
			row.createCell(8).setCellValue("否");
			else  row.createCell(8).setCellValue("是");
			row.createCell(9).setCellValue(creditsExchange.getReason());
			row.createCell(10).setCellValue(creditsExchange.getRemark());
			row.createCell(11).setCellValue(creditsExchange.getOperTime());
			row.createCell(12).setCellValue(creditsExchange.getIP());
			 
		}
	}
	
//	headers.add("用户ID");
//	headers.add("用户昵称");
//	headers.add("邮箱");
//	headers.add("手机号");
//	headers.add("总积分");
//	headers.add("兑换积分");
//	headers.add("剩余积分"); 
//	headers.add("注册时间"); 
	/**
	 * 积分兑换记录
	 * @param sheet
	 * @param list
	 */
	public void usreAdd( HSSFSheet sheet,List<User> list){
		int rowNum = 1;
		for(User user : list){
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(user.getUserid());
			row.createCell(1).setCellValue(user.getUsername());
			row.createCell(2).setCellValue(user.getEamil());
			row.createCell(3).setCellValue(user.getPhone());
			row.createCell(4).setCellValue(user.getIntegral());
			row.createCell(5).setCellValue(user.getHuIntegral());
			row.createCell(6).setCellValue(user.getIntegral()-user.getHuIntegral());
			row.createCell(7).setCellValue(user.getRegister().substring(2));
			row.createCell(8).setCellValue(user.getIP());
			row.createCell(9).setCellValue(user.getVersion());
			row.createCell(10).setCellValue(user.getZfb1());
			row.createCell(11).setCellValue(user.getCft1());
			row.createCell(12).setCellValue(user.getQq1());
			row.createCell(13).setCellValue(user.getPhone1());
		}
	}
	
//	headers.add("用户ID");
//	headers.add("用户昵称");
//	headers.add("获得积分");
//	headers.add("时间"); 
//	headers.add("类型"); 
//	headers.add("备注");
	
	/**
	 * 兑换获取记录
	 * @param sheet
	 * @param list
	 */
	public void integralAdd( HSSFSheet sheet,List<ObtainIntegral> list){
		int rowNum = 1;
		for(ObtainIntegral obtainIntegral : list){
			HSSFRow row = sheet.createRow(rowNum++);
			row.createCell(0).setCellValue(obtainIntegral.getUserid());
			row.createCell(1).setCellValue(obtainIntegral.getUsername());
			row.createCell(2).setCellValue(obtainIntegral.getNumber());
			row.createCell(3).setCellValue(obtainIntegral.getFnumber());
			row.createCell(4).setCellValue(obtainIntegral.getCrdate().substring(2));
			String typestr=obtainIntegral.getType()==1?"自己获取":"管理员增减"; //1自己获取2管理员增减
			row.createCell(5).setCellValue(typestr);
			row.createCell(6).setCellValue(obtainIntegral.getRemark());
			row.createCell(7).setCellValue(obtainIntegral.getIP());
			row.createCell(8).setCellValue(obtainIntegral.getVersion());
		}
	}
}